<!-- 个人中心的地址管理的新增收货地址 -->
<template>
	<view class="page">
		<view class="address-input">
			<view class="list-input">
				<view class="title">
					<text>收货人:</text>
				</view>
				<view class="content">
					<input type="text" placeholder="请填写收货人姓名" v-model="AddressTable .userName">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>手机号:</text>
				</view>
				<view class="content">
					<input type="tel" placeholder="请填写收货人手机号" v-model="AddressTable .userPhone">
				</view>
			</view>
			<view class="list-input">
				<view class="title">
					<text>收货小区:</text>
				</view>
				<view class="content">
					<input type="tel" placeholder="请点击进行选择" v-model="AddressTable .userMapAddress" @click="addressChoice" disabled>
				</view>
			</view>
			<view class="list-textarea">
				<view class="title">
					<text>详细地址:</text>
				</view>
				<view class="content">
					<textarea type="tel" placeholder="街道/楼牌号等" v-model="AddressTable.address" />
				</view>
			</view>
		</view>
		<view class="tag-default">
			<view class="default-address">
				<view class="title">
					<text>默认地址:</text>
				</view>
				<view class="switch-default">
					<switch @change="switchChange" class="red sm" color="#0077EE !important" :checked="checked" ></switch>
				</view>
			</view>
		</view>
		<span v-if="addressType=='1'">
		<view class="footer-btn">
			<view class="btn" @click="preservation(AddressTable.addressId)">
				<text>保存</text>
			</view>
		</view>
		</span>
		<span v-if="addressType=='2'">
		<view class="footer-btn">
			<view class="btn" @click="add()">
				<text>添加</text>
			</view>
		</view>
		</span>
	</view>
</template>

<script>
	import {baseUrl} from '@/config/baseUrl.js'
	export default {
		data() {
			return {
				addressType: '2',
				checked: false,
				AddressTable:{
					address:'',
					userName:'',
					userPhone:'',
					userMapAddress:'',
					userAddressLat:'',
					userAddressLng:'',
					state:'',
				}
			};
		},
		onLoad(params) {
			this.addressType = params.type||'2';
			uni.setNavigationBarTitle({
				title: this.addressType === '1' ? '编辑收货地址':'新建收货地址'
			})
			if(this.addressType === '1'){
				this.AddressTable = uni.getStorageSync("AddressTable");
			}else{
				this.AddressTable.userAddressLat = uni.getStorageSync("latitude");
				this.AddressTable.userAddressLng = uni.getStorageSync("longitude");
			}
			if(this.AddressTable.state==34){
				this.checked=true
			}
		},
		methods:{
			addressObj(selectAddr){
				this.AddressTable.userMapAddress = selectAddr.address
				this.AddressTable.userAddressLat = selectAddr.latitude
				this.AddressTable.userAddressLng = selectAddr.longitude
			},
			addressChoice(){
					uni.navigateTo({
						url: '/pages/UserMap/UserMap?lat='+this.AddressTable.userAddressLat+'&lng='+this.AddressTable.userAddressLng
					})
			},
			preservation(id){
				uni.request({
					url:  baseUrl+"/user/updateUserAddress",
					method: 'POST',
					data: {
						addressId:id,
						address:this.AddressTable.address,
						userName:this.AddressTable.userName,
						userPhone:this.AddressTable.userPhone,
						userMapAddress:this.AddressTable.userMapAddress,
						userAddressLat:this.AddressTable.userAddressLat,
						userAddressLng:this.AddressTable.userAddressLng,
						state:this.AddressTable.state
					},
					header:{ "Content-Type": "application/x-www-form-urlencoded"},
					success: (res) => {
						console.log(res);
						if(res.data >0){
							uni.showToast({
								title: "修改成功!",
								icon: "none",
								duration: 3000,
							})
							uni.navigateTo({
								url: '/pages/AddressList/AddressList'
							})
						}else{
							uni.showToast({
								title: "修改失败，请检查网络是否正常",
								icon: "none",
								duration: 3000,
							})
						}
					}
				})
			},
			add(){
				uni.request({
					url:  baseUrl+"/user/insertUserAddress",
					method: 'POST',
					data: {
						userId:uni.getStorageSync("userId"),
						address:this.AddressTable.address,
						userName:this.AddressTable.userName,
						userPhone:this.AddressTable.userPhone,
						userMapAddress:this.AddressTable.userMapAddress,
						userAddressLat:this.AddressTable.userAddressLat,
						userAddressLng:this.AddressTable.userAddressLng,
						state:this.AddressTable.state
					},
					header:{ "Content-Type": "application/x-www-form-urlencoded"},
					success: (res) => {
						console.log(res);
						if(res.data >0){
							uni.showToast({
								title: "添加成功!",
								icon: "none",
								duration: 3000,
							})
							uni.reLaunch({
								url: '/pages/AddressList/AddressList'
							})
						}else{
							uni.showToast({
								title: "添加失败，请检查网络是否正常",
								icon: "none",
								duration: 3000,
							})
						}
					}
				})
			},
			state(){
				if(this.AddressTable.state==0){
					this.AddressTable.state=1
				}else{
					this.AddressTable.state=0
				}
				console.log(this.AddressTable.state)
			},
			switchChange() {
				if(this.checked){
					this.checked = false
					this.AddressTable.state = 35
				}else{
					this.checked = true
					this.AddressTable.state = 34
				}
				console.log(this.checked)
				console.log(this.AddressTable.state)
				
		},
		}
	}
</script>

<style scoped lang="scss">
	@import 'AddressEdit.scss';
</style>
